<?php

namespace App\Repositories;

use App\Exceptions\BasicException;
use App\Services\Tools\FilterService;
use Illuminate\Http\Request;

class UserDeviceRepository
{
    public function getLastDevice($user_id)
    {
        $lastDevice = app('model_user_device')->where("user_id", $user_id)->orderBy('id', 'desc')->value('device_id');
        return $lastDevice ?? '';
    }

    public function checkRegisterUser($userId, $deviceId, $boxId, $phone = '')
    {
        if (empty($userId)) {
            return true;
        }
        $config = app("model_config")->getValue('SHUMEI_CHECK_REGISTER');
        if (empty($config)) { // 配置没开启
            return true;
        }
        $config          = json_decode($config, true);
        $mustConfigKey   = ['check_phone', 'check_device', 'handle_user', 'handle_device'];
        $configIntersect = array_intersect($mustConfigKey, array_keys($config));
        if (count($configIntersect) != count($mustConfigKey)) { // 配置错误
            return true;
        }
        $checkPhone   = $config['check_phone'];
        $checkDevice  = $config['check_device'];
        $handleUser   = $config['handle_user'];
        $handleDevice = $config['handle_device'];

        if ($checkPhone != 1 && $checkDevice != 1) {
            return true;
        }
        if ($handleUser != 1 && $handleDevice != 1) {
            return true;
        }
        if (!$checkPhone) {
            $phone = '';
        } else {
            if (empty($phone)) {
                $phone = app('model_user')->where('user_id', $userId)->value('mobile');
            }
        }
        if (!$checkDevice) {
            $boxId = '';
        }
        $checkResult = FilterService::checkUserRegister($userId, $boxId, $phone);
        if (!$checkResult) {
            return true;
        }
        if ($handleUser) {
            $this->disableUser($userId, $checkResult);
        }
        if ($handleDevice) {
            $this->disableDevice($deviceId, $checkResult);
        }
        return true;
    }

    public function disableDevice($deviceId, $remark = '')
    {
        $devicePrimaryId     = app('model_user_device')->where('device_id', $deviceId)->value('id');
        $sealDeviceLogData[] = [
            'user_id'   => 0,
            'remark'    => '系统自动校验注册不合法封禁:' . $remark,
            'admin_id'  => 0,
            'device_id' => $devicePrimaryId,
            'add_time'  => time(),
        ];
        app('model_user_device_ban_log')->insert($sealDeviceLogData);
        app('redis')->sadd('seal:device', $deviceId);
        app('model_user_device')->where('device_id', $deviceId)->update(['state' => 0, 'seal_time' => time()]);
        return true;
    }

    public function disableUser($userId, $remark = '')
    {
        $sealUserLogData = [
            'user_id'   => $userId,
            'seal_time' => time() + time(),
            'add_time'  => time(),
            'remark'    => '系统自动校验注册不合法封禁' . $remark,
            'admin_id'  => 0,
        ];
        app('redis')->set("u:seal:{$userId}", time() + time());
        app('model_user_seal')->insert($sealUserLogData);
        app('model_user')->where('user_id', $userId)->update(['disabled' => 0]);
        return true;
    }

}
